package com.atguigu.lease.web.admin.controller.apartment;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.AttrKey;
import com.atguigu.lease.model.entity.AttrValue;
import com.atguigu.lease.web.admin.service.AttrKeyService;
import com.atguigu.lease.web.admin.service.AttrValueService;
import com.atguigu.lease.web.admin.vo.attr.AttrKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name="房间属性信息管理")
@RestController
@RequestMapping("/admin/attr")
public class AttrInfoController {

    @Autowired
    AttrKeyService attrKeyService;
    @Autowired
    AttrValueService attrValueService;

    @Operation(summary = "查询全部属性名称和属性值列表")
    @GetMapping("list")
    public Result attrInfo(){
        List<AttrKeyVo> attrKeyVoList = attrKeyService.attrInfoList();
        return Result.ok(attrKeyVoList);
    }

    @Operation(summary ="新增或更新属性名称")
    @PostMapping("key/saveOrUpdate")
    public Result saveOrUpdateAttrKey(@RequestBody AttrKey attrKey){
        attrKeyService.saveOrUpdate(attrKey);
        return Result.ok();
    }

    @Operation(summary ="新增或更新属性值")
    @PostMapping("value/saveOrUpdate")
    public Result saveOrUpdateAttrValue(@RequestBody AttrValue attrValue){
        attrValueService.saveOrUpdate(attrValue);
        return Result.ok();
    }

    @Operation(summary ="根据id删除属性名称")
    @DeleteMapping("key/deleteById")
    public Result deleteAttrKeyById(@RequestParam Long attrKeyId){
        //先删除attrkey
        attrKeyService.removeById(attrKeyId);

        //根据attrkey删除对应的每一个attrvalue，相当于三行代码执行这一步
        LambdaQueryWrapper<AttrValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrValue::getAttrKeyId, attrKeyId);
        // 设置查询条件：AttrValue.getAttrKeyId = 传入进来的attrKeyId
        attrValueService.remove(wrapper);
        //attrValueService.remove(AttrKeyId);
        //这个AttrKeyId的值是等于传进来的attrKeyId

        return Result.ok();
    }

    @Operation(summary ="根据id删除属性值")
    @DeleteMapping("value/deleteById")
    public Result deleteAttrValueById(@RequestParam Long id){
        attrValueService.removeById(id);
        return Result.ok();
    }
}
